package com.js.study.datastructs.version2;

/**
 * @Author JiangSen
 * @DATE 2023/12/23 18:10
 */
public class MyArray<E> implements IArray<E>{
    E[] data;
    int size;

    public MyArray(){
        this(5);
    }
    public MyArray(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public int getCapacity() {
        return data.length;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == data.length;
    }

    @Override
    public void addFirst(E e) {
        add(0, e);
    }

    @Override
    public void addLast(E e) {
        add(size,e);
    }

    @Override
    public void add(int index, E e) {
        if(index < 0 || index >getCapacity()){
            throw new RuntimeException("index is not in size");
        }
        if(isFull()){
            resize(getCapacity()*2);
        }
        for(int i = size -1; i>=index; i--){
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }

    @Override
    public void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < getSize(); i++) {
            newData[i] = data[i];
        }
        data = newData;//改变引用地址
    }

    @Override
    public E removeFirst() {
        return remove(0);
    }

    @Override
    public E remove(int index) {
        if (isEmpty()) {
            throw new RuntimeException("数组为空，不能删除元素");
        }

        if (index < 0 || index >= size) {
            throw new RuntimeException("索引不在范围边界内");
        }

        E result = data[index]; //先获取要删除的索引对应的元素，方法返回
        for(int i = index +1;i< size; i++){
            data[i+1] = data[i];
        }
        size--;
        data[size] =null;//如果不置空,会出现游离对象
        //缩小容量
        if(getSize() == getCapacity()/2){
            System.out.println("resize");
            resize(getCapacity()/2);
        }

        return result;
    }

    @Override
    public E removeLast() {
        return remove(size - 1);
    }

    @Override
    public E set(int index, E e) {
        if(index < 0 || index >=getCapacity()){
            throw new RuntimeException("index is not in size");
        }
        return data[index]=e;
    }

    @Override
    public E get(int index) {
        if(index < 0 || index >=getCapacity()){
            throw new RuntimeException("index is not in size");
        }
        return data[index];
    }

    @Override
    public boolean cotain(E e) {
        for(int i =0; i<size; i++){
            if(data[i]== e){
                return true;
            }
        }
        return false;
    }

    @Override
    public void pritn() {
        for (int i = 0; i < data.length; i++) { //游离对象,后面解决
            System.out.println("data["+i+"] = " + data[i]);
        }
    }

    @Override
    public void printReverse() {
        for (int i = size - 1; i >= 0; i--) {
            System.out.println("data[i] = " + data[i]);
        }
    }
}
